import pygame

class CHUD(object):
    """ Holds and displays hud information. """
    
    mLogMessages = []
    mStats = []
    mFont = None
    
    STAT_HEALTH = 0
    STAT_GOLD = 2
    STAT_LEVEL = 3
    
    def __init__(self):
        # Create our font to use
        CHUD.mFont = pygame.font.Font( "freesansbold.ttf" , 11 )
        
        # Create our title texts
        self.mLogTitle = self.mFont.render( 'Log: ', True , ( 255 , 255 , 255 ) , ( 159 , 182 , 205 ) )
        self.mStatsTitle = self.mFont.render( 'Stats: ' , True , ( 255 , 255 , 255 ) , ( 159 , 182 , 205 ) )
        
        # Create our stat texts
        self.mHealthTitle = self.mFont.render( 'Health:' , True , ( 255 , 255 , 255 ) , ( 99 , 182 , 88 ) )
        self.mWeaponTitle = self.mFont.render( 'Weapon:' , True , ( 255 , 255 , 255 ) , ( 159 , 102 , 205 ) )
        self.mGoldTitle = self.mFont.render( 'Gold:' , True , ( 255 , 255 , 255 ) , ( 223 , 190 , 102 ) )
        self.mLevelTitle = self.mFont.render( 'Level:' , True , ( 255 , 255 , 255 ) , ( 159 , 102 , 120 ) )
        
        # Create our log List full of empty text
        blankText = self.mFont.render( '', True , ( 255 , 255 , 255 ) , ( 159 , 182 , 205 ) )
        CHUD.mLogMessages = [blankText] * 4
        
        # Create our empty stats list
        CHUD.mStats = [blankText] * 4
        
        # Create our title screen text holder
        self.mTitleScreenText = [blankText] * 4
 


    def draw(self , inScreen):
        """ Draw our HUD """
        
        # Create base rectangles
        pygame.draw.rect( inScreen , ( 122 , 122 , 122 ) , ( 0 , 480-64 , 320 , 64 ) )
        pygame.draw.rect( inScreen , ( 90 , 122 , 105 ) , ( 320 , 480-64 , 320 , 64 ) )
        
        # Draw titles
        inScreen.blit( self.mLogTitle , ( 5 , 412  ) )
        inScreen.blit( self.mStatsTitle , ( 330 , 412 ) )
        
        # Draw stat titles
        inScreen.blit( self.mHealthTitle , ( 339 , 426 ) )
        inScreen.blit( self.mWeaponTitle , ( 330 , 440 ) )
        inScreen.blit( self.mGoldTitle, ( 348 , 454 ) )
        inScreen.blit( self.mLevelTitle, ( 348 , 468 ) )
        
        # Draw Log Text
        i = 430
        for message in CHUD.mLogMessages:
            inScreen.blit( message , ( 5 , i ))
            i += self.mFont.get_linesize()
        
        # Draw Stat Info - Todo: If more time we could combine this with the titles using the list.
        i = 426
        for stat in CHUD.mStats:
            inScreen.blit( stat , ( 387 , i))
            i += 14
            
    def DrawTitleScreen(self , inScreen ):
        """ Draw a title screen that displays some text in a box. """
        pygame.draw.rect( inScreen , ( 90 , 122 , 105 ) , ( 320-200 , 240-100 , 400 , 100 ) )
        
        i = 150
        for text in self.mTitleScreenText:
            inScreen.blit(text , ( 140 , i ) )
            i += self.mFont.get_linesize() + 10
        
    def SetTitleScreenText(self , textList ):
        """ Set title screen text as a list of three strings, one for each line. """
        for idx , text in enumerate(textList):
            self.mTitleScreenText[idx] = blankText = self.mFont.render( str(text) , True , ( 255 , 255 , 255 ) , ( 90 , 122 , 105 ) )
            
    @staticmethod
    def LogMessage(m):
        """ Sends a log message to the HUD. Note that this will not force a draw() call so doing this without dirtying the canvas will lead to a non-immediate update. """
        
        # Pop our oldest message to keep our buffer 4 lines high
        CHUD.mLogMessages.pop(0)
            
        CHUD.mLogMessages.append( CHUD.mFont.render( m , True , ( 255 , 255 , 255 ) , ( 159 , 182 , 205 ) ) )
        
    @staticmethod
    def ChangeStat( stat , value ):
        """ Changes a given stat in the HUD ( STAT_HEALTH , STAT_GOLD , STAT_LEVEL ). Call looks like ChangeStat( CHUD.STAT_GOLD , 10 ). Change weapon info with the ChangeWeaponStat() function. """
        CHUD.mStats[stat] = CHUD.mFont.render( str(value) , True , (255 , 255 , 255 ) , ( 90 , 122 , 105 )  )
            
    @staticmethod
    def ChangeWeaponStat( name , attack ):
        """ Change the weapon stat in the hud based on the weapon's name and attack power. """
        CHUD.mStats[1] = CHUD.mFont.render( name + " (+" + str(attack) + " ATK)" , True , (255 , 255 , 255 ) , ( 90 , 122 , 105 )  )
        
    
        
        
         

